package com.safe.ly_safe.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.util.HashMap;

/**
 * @PACKAGE :com.safe.ly_safe.utils
 * @Author : HSF
 * @Time : On 2022/4/25 9:40
 * @Description : SharedUtils
 */
public class SharedUtils {

    public static String Shared_File_Name = "lySafe_data";

    public static final String KEY_IS_FIRST_IN = "is_first_in"; //第一次运行APP

    public static final String LOGIN_USER_NAME = "login_user_name"; //登录用户名

    public static final String LOGIN_PASS_WORD = "login_pass_word"; //登录密码

    public static final String KEY_HAS_LOGIN_IN = "is_has_login_in"; //已经登录成功了

    @SuppressLint("ApplySharedPref")
    public static void putSingleInfo(Context context, String mKey, Object mValue){

        SharedPreferences sharedPreferences = context.getSharedPreferences(Shared_File_Name, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sharedPreferences.edit();

        if (mValue instanceof String) {

            editor.putString(mKey, (String) mValue);

        } else if (mValue instanceof Integer) {

            editor.putInt(mKey, (Integer) mValue);

        } else if (mValue instanceof Boolean) {

            editor.putBoolean(mKey, (Boolean) mValue);

        } else if (mValue instanceof Float) {

            editor.putFloat(mKey, (Float) mValue);

        } else if (mValue instanceof Long) {

            editor.putLong(mKey, (Long) mValue);

        } else {

            editor.putString(mKey, mValue.toString());

        }

        editor.commit();
    }

    @SuppressLint("ApplySharedPref")
    public static void putMoreInfo(Context context, HashMap<String,Object> sharedMap){

        SharedPreferences sharedPreferences = context.getSharedPreferences(Shared_File_Name, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sharedPreferences.edit();

        if(sharedMap != null && sharedMap.size() > 0){
            for(String key:sharedMap.keySet()) {
                Object mValue = sharedMap.get(key);

                Log.i("SharedUtils","Key::"+key);
                Log.i("SharedUtils","Value::"+mValue);

                if (mValue instanceof String) {

                    editor.putString(key, (String) mValue);

                } else if (mValue instanceof Integer) {

                    editor.putInt(key, (Integer) mValue);

                } else if (mValue instanceof Boolean) {

                    editor.putBoolean(key, (Boolean) mValue);

                } else if (mValue instanceof Float) {

                    editor.putFloat(key, (Float) mValue);

                } else if (mValue instanceof Long) {

                    editor.putLong(key, (Long) mValue);

                } else {

                    editor.putString(key, mValue.toString());

                }
            }
        }
        editor.commit();
    }

    /**
     * 得到保存数据，根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     * defaultObject: 默认值
     */
    public static Object getSharedInfo(Context context, String key, Object defaultObject) {

        SharedPreferences sp = context.getSharedPreferences(Shared_File_Name, Context.MODE_PRIVATE);

        if (defaultObject instanceof String) {

            return sp.getString(key, (String) defaultObject);

        } else if (defaultObject instanceof Integer) {

            return sp.getInt(key, (Integer) defaultObject);

        } else if (defaultObject instanceof Boolean) {

            return sp.getBoolean(key, (Boolean) defaultObject);

        } else if (defaultObject instanceof Float) {

            return sp.getFloat(key, (Float) defaultObject);

        } else if (defaultObject instanceof Long) {

            return sp.getLong(key, (Long) defaultObject);

        }
        return null;
    }

    /**
     * 清除所有数据
     * @param context
     */
    @SuppressLint("ApplySharedPref")
    public static void clear(Context context) {
        SharedPreferences sp = context.getSharedPreferences(Shared_File_Name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }
}
